from django.shortcuts import render
from django.http import HttpResponse
import requests
from lxml import etree
import os
from fake_useragent import UserAgent
from time import sleep
import random

# Create your views here.
def get_new_board(request):
    '''
        获取新片榜
    :param request:
    :return:
    '''

    def get_xml(url):
        '''
            获取页面，返回xml页面对象
        '''

        res = requests.get(url,
                           headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'})
        html = res.content.decode('utf-8')
        p = etree.HTML(html)
        return p

    def get_movie(url):
        '''
            获取图片、片名、影片信息、链接
        '''
        q = get_xml(url)
        get_name = '//td/div/a/span/text()'
        get_name_href = '//td/div/a/@href'
        get_name_intro = '//p[@class="pl"]/text()'
        get_name_picture = '//td/a/img/@src'
        bname = q.xpath(get_name)
        bhref = q.xpath(get_name_href)
        bintro = q.xpath(get_name_intro)
        bpic = q.xpath(get_name_picture)
        bpic2 = []
        for item in bpic:
            item2 = str(item[:-4])+'.webp'
            bpic2.append(item2)
        return bname, bhref, bintro, bpic2
    one_name, one_href, one_intro, one_pic = get_movie('https://movie.douban.com/chart')
    movie = zip(one_name,one_href,one_intro,one_pic)
    return render(request, 'new_movie_board.html', locals())
def get_reputation_board(request):
    '''
    获取一周口碑榜
    :param request:
    :return:
    '''
    def get_xml(url):
        '''
            获取页面，返回xml页面对象
        '''

        res = requests.get(url,
                           headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'})
        html = res.content.decode('utf-8')
        p = etree.HTML(html)
        return p

    def get_one(url):
        '''
            获取一级页面片名、链接
        '''
        q = get_xml(url)
        get_name = '//*[@id="listCont2"]/li/div/a/text()'
        get_name_href = '//*[@id="listCont2"]/li/div/a/@href'
        bname = q.xpath(get_name)
        bhref = q.xpath(get_name_href)
        return bname, bhref
    def get_two(url):
        q = get_xml(url)
        get_two_name = '//*[@id="content"]/h1/span[1]/text()'
        get_two_intro = '//*[@id="link-report"]/span/text()'
        get_two_pic = '//*[@id="mainpic"]/a/img/@src'

        two_name = q.xpath(get_two_name)
        two_href = url
        two_intro = q.xpath(get_two_intro)
        # 删除简介中无关内容
        two_intro2 = []
        for item in two_intro:
            item2 = item[35:-25]
            two_intro2.append(item2)
        two_pic = q.xpath(get_two_pic)
        # 更改图片格式
        two_pic2 = []
        for item in two_pic:
            item2 = str(item[:-4])+'.webp'
            two_pic2.append(item2)



        return two_name, two_href, two_intro2, two_pic2

    one_name, one_href = get_one('https://movie.douban.com/chart')
    two_name = []
    two_href = []
    two_intro = []
    two_pic = []
    for item in range(len(one_name)):
        # 获取每个电影的信息并分别存入列表中
        temp_name, temp_href, temp_intro, temp_pic = get_two(one_href[item])
        for item in temp_name:
            two_name.append(item)
        for item in temp_intro:
            two_intro.append(item)
        for item in temp_pic:
            two_pic.append(item)
    movie = zip(two_name,one_href,two_intro,two_pic)
    return render(request, 'weekly_reputation_board.html', locals())




